﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Core;
using System.Collections.Generic;
using Exportation;
using Core.DAL;
using System.Transactions;

namespace xManagerWebApp
{
    public partial class ControlExportation : System.Web.UI.UserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            this.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //String s = this.ddl_ExportationTypes.SelectedValue;
            //this.DataBind();
            //s = this.ddl_ExportationTypes.SelectedValue;
        }

        public override void DataBind()
        {
            //using (DataAccessDataContext dc = new DataAccessDataContext())
            //{
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                this.ddl_ExportationTypes.DataSource = ExportationType.GetAll(dal);
                this.ddl_ExportationTypes.DataBind();
                //    var query = from t in dc.ExportationTypes
                //                select t;
                //    IEnumerable<ExportationType> etypes = query.AsEnumerable<ExportationType>();
                //    List<ExportationType> l = etypes.ToList<ExportationType>();
                //    ExportationType[] arr = new ExportationType[] { };

                //this.ddl_ExportationTypes.DataSource = etypes;
                //var query = from er in dc.ExpenseReports
                //            where !(from e in dc.ExpenseReports
                //                from t in dc.ExpenseExportations
                //                where t.ExpenseReport.Equals(e) && t.Type.Equals("ListingExcelll")
                //                select e).Contains<ExpenseReport>(er)
                //            select er;

                //this.LinqDataSource1.Where =
                //base.DataBind();
            }
            //}
        }

        protected void bt_Export_Click(object sender, EventArgs ea)
        {
            /*Logo por azar: 
             * The ITransactionLocal interface is not supported by the 'Microsoft.Jet.OLEDB.4.0' provider.  
             * Local transactions are unavailable with the current provider.*/
            
            //using (TransactionScope ts = new TransactionScope())
            //{
                foreach (GridViewRow row in this.gv_reports.Rows)
                {
                    CheckBox cb = (CheckBox)row.FindControl("ReportsSelector");
                    if (cb != null && cb.Checked)
                    {
                        Guid g = (Guid)this.gv_reports.DataKeys[row.RowIndex].Value;
                        using (DataAccessLayer dal = new DataAccessLayer())
                        {
                            ExpenseReport er = ExpenseReport.Get(dal, g);
                            if (er != null)
                                er.Export(dal, this.ddl_ExportationTypes.SelectedValue);
                        }
                    }
                }
            //    ts.Complete();
            //}
        }

        protected void OnLinqDataSourceSelecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                var query = Core.ExportatedReportsView.LoadSearchResult(dal, this.ddl_ExportationTypes.SelectedValue);
                e.Result = query;
            }
        }

        protected void ddl_ExportationTypes_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected override void OnPreRender(EventArgs e)
        {
            this.gv_reports.DataBind();
            //base.OnPreRender(e);
            //String s = this.ddl_ExportationTypes.SelectedValue;
            //this.DataBind();
        }
    }
}